#ifndef VL_BLOCK_QUEUE__H_
#define VL_BLOCK_QUEUE__H_

#include <memory>
#include <deque>
#include "vl_semphore.h"

namespace VL {

template <class T>
class BlockQueue {
public:
	BlockQueue() {}
	~BlockQueue() {}

	void push(T* dataPtr) {
		this->semphore.lock();
		this->deque.push_back(std::shared_ptr<T>(dataPtr));
		this->semphore.unlock();
	}
	
	void push(std::shared_ptr<T> dataPtr) {
		this->semphore.lock();
		this->deque.push_back(dataPtr);
		this->semphore.unlock();
	}

	std::shared_ptr<T> pop() {
		this->semphore.lock();
		if(this->deque.empty()) {
			this->semphore.unlock();
			return std::shared_ptr<T>(nullptr);
		}
		std::shared_ptr<T> dataPtr(this->deque.front());
		this->deque.pop_front();
		this->semphore.unlock();
		return dataPtr;
	}

	bool isEmpty() {
		bool isEmpty;
		this->semphore.lock();
		isEmpty = this->deque.empty();
		this->semphore.unlock();
		return isEmpty;
	}

	void clear() {
		this->semphore.lock();
		this->deque.clear();
		this->semphore.unlock();
	}

private:
	Semphore semphore;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-02-07 16:16:14
	 * 
	 * 阻塞队列
	 */
	std::deque< std::shared_ptr<T> > deque;
};


}

#endif //VL_BLOCK_QUEUE__H_
